/**
 * 按汉字个数拆分字符串 str，每个数组元素长度不超过 num
 * 两个 ASCII 算一个汉字
 * @param {string} str
 * @param {number} num
 * @returns {string[]}
 */
export function splitByHanziNum(str, num) {
    let begin = 0;
    let end = 0;
    let currentLength = 0;
    const result = [];
    while (end < str.length) {
        const char = str[end];
        if (char.codePointAt(0) <= 128) {
            currentLength += 0.5;
        } else {
            currentLength += 1;
        }
        if (currentLength > num) {
            result.push(str.slice(begin, end));
            begin = end;
            currentLength = 1;
        }

        end++;
    }

    result.push(str.slice(begin));

    return result;
}
